﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RayDen.Library.Core.Primitives;

namespace RayDen.MaterialManager.Controls
{
    public partial class EditSpectrumForm : Form, IEditForm<RgbSpectrum>
    {
        private bool setValue;

        public RgbSpectrum editedValue;

        public string ValueName { get; set; }
        public RgbSpectrum Value { get; set; }

        protected Color CurrentColor
        {
            get { return this.Value.ToRgb().ToColor(); }
        }

        public EditSpectrumForm()
        {
            InitializeComponent();
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.BindForm();
        }


        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            this.Value = new RgbSpectrum(GetSPD());
            this.DialogResult = setValue ? DialogResult.OK : DialogResult.Cancel;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            setValue = false;
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Value = editedValue;
            setValue = true;
            this.Close();
        }

        private void btnOpenDialog_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.Value = RgbSpectrum.Create(colorDialog1.Color);
                this.BindForm();
            }
        }


        private void BindForm()
        {
            this.txtValueName.Text = this.ValueName;
            var xyzValue = this.Value.XYZ();
            var spdValue = this.Value.ToArray();
            var rgbValue = this.Value.ToRgb().ToColor();

            this.txtX.Text = xyzValue[0].ToString();
            this.txtY.Text = xyzValue[1].ToString();
            this.txtZ.Text = xyzValue[2].ToString();



            this.txtC1.Text = spdValue[0].ToString();
            this.txtC2.Text = spdValue[1].ToString();
            this.txtC3.Text = spdValue[2].ToString();

            this.txtR.Text = rgbValue.R.ToString();
            this.txtG.Text = rgbValue.G.ToString();
            this.txtB.Text = rgbValue.B.ToString();
            this.Refresh();
        }

        private void ctlColorCanvas_Paint(object sender, PaintEventArgs e)
        {
            using (var p = new SolidBrush(this.CurrentColor))
            {
                e.Graphics.FillRectangle(p, e.ClipRectangle);
            }
            ctlColorCanvas.Refresh();
        }

        private void chkGamma_CheckedChanged(object sender, EventArgs e)
        {
            RgbSpectrum.Gamma = chkGamma.Checked;
        }

        private void btnCreate4Rgb_Click(object sender, EventArgs e)
        {
            var rgb = GetRGB();
            this.Value = RgbSpectrum.Create(rgb[0], rgb[1], rgb[2], chkColorCorrection.Checked);
            this.BindForm();
        }
        private void btnCreate4XYZ_Click(object sender, EventArgs e)
        {
            var rgb = GetXYZ();
            this.Value = RgbSpectrum.FromXYZ(rgb);
            this.BindForm();
        }
        private float[] GetXYZ()
        {
            float x, y, z;
            float.TryParse(txtX.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out x);
            float.TryParse(txtY.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out y);
            float.TryParse(txtZ.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out z);
            return new [] {x,y,z};
        }
        private byte[] GetRGB()
        {
            float x, y, z;
            float.TryParse(txtR.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out x);
            float.TryParse(txtG.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out y);
            float.TryParse(txtB.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out z);
            return new[] { (byte)((255f)*x),
                           (byte)((255f)*y),
                           (byte)((255f)*z) };
        }
        private float[] GetSPD()
        {
            float x, y, z;
            float.TryParse(txtC1.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out x);
            float.TryParse(txtC2.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out y);
            float.TryParse(txtC3.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out z);
            return new[] { x, y, z };
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.BindForm();
            this.Update();
        }

        private void btnCreate4SPD_Click(object sender, EventArgs e)
        {
            var rgb = GetSPD();
            this.Value = new RgbSpectrum(rgb);
            this.BindForm();
        }

    
    }
}
